const path = require('path')

const resolve = dir => {
  return path.join(__dirname, dir)
}

process.env.VUE_APP_VERSION = require('./package.json').version

module.exports = {
  productionSourceMap: false, // 不需要source map文件
  publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
  devServer: {
    disableHostCheck: true,
    port: 8092,
    proxy: {
      '/api': {
        target: process.env.VUE_APP_PROXY_URL,
        // ws: true, // 代理websocket
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  },
  chainWebpack: config => {
    // 移除 prefetch 插件
    config.plugins.delete('prefetch')
    // 配置别名
    config.resolve.alias
      .set('@', resolve('src'))
      .set('_c', resolve('src/components'))
      .set('_v', resolve('src/views'))
      .set('_img', resolve('src/assets'))
    // 注入cdn
    // config.plugin('html').tap(args => {
    //   args[0].cdn = cdn
    //   return args
    // })
    if (process.env.NODE_ENV === 'production') {
      // webpack打包分析
      if (process.env.use_analyzer) {
        config
          .plugin('webpack-bundle-analyzer')
          .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
      }
    }
  },
  configureWebpack: config => {
    // 用cdn方式引入
    // config.externals = {
    //   'wx': 'wx'
    // }
    if (process.env.NODE_ENV === 'production') {
      // 去除console和debugger
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
      config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true
      // 删除代码注释
      config.optimization.minimizer[0].options.terserOptions.output.comments = false
    }
  },
  pluginOptions: {
    // 在所有.vue  .styl   .stylus  文件中引入 variable、mixins
    'style-resources-loader': {
      preProcessor: 'stylus',
      patterns: [
        path.resolve(__dirname, './src/styles/variable.styl'),
        path.resolve(__dirname, './src/styles/mixins.styl')
      ]
    }
  }
}
